<?php 
$conn = oci_connect("gaurav", "gaurav", "xe"); 


$query = 'SELECT * FROM Messages where messageid = :mesgid';
$bindargs = array();
array_push($bindargs, array('mesgid', 1, -1));
$msg = db_do_query($conn, $query, $bindargs);
ui_print_message($msg[0]);

// Execute query and display results 
function db_do_query($conn, $statement, $bindvars = array())
{
  $stid = oci_parse($conn, $statement);
  if (!$stid) {
    db_error($conn, __FILE__, __LINE__);
  }

  // Bind the PHP values to query bind parameters
  foreach ($bindvars as $b) {
    // create local variable with caller specified bind value
    $$b[0] = $b[1];  
    // oci_bind_by_name(resource, bv_name, php_variable, length)
    $r = oci_bind_by_name($stid, ":$b[0]", $$b[0], $b[2]); 
    if (!$r) {
      db_error($stid, __FILE__, __LINE__);
    }
  }
  $r = oci_execute($stid, OCI_DEFAULT);
  if (!$r) {
        db_error($stid, __FILE__, __LINE__);
  }
  $r = oci_fetch_all($stid, $results, null, null,
                         OCI_FETCHSTATEMENT_BY_ROW);
  return($results);
}

function db_error($r = false, $file, $line)
{
  $err =  $r ? oci_error($r) : oci_error();

  if (isset($err['message'])) {
    $m = htmlentities($err['message']);
  }
  else {  
    $m = 'Unknown DB error';
  }

  echo '<p><b>Error</b>: at line '.$line.' of '.$file.'</p>';
  echo '<pre>'.$m.'</pre>';

  exit;
}

function ui_print_message($msg)
{
  if (!$msg) {
    echo '<p>No Messages found</p>';
  }
  else {
    echo <<<END
  <table border="1">
  <tr>
    <th>MessageID</th>
    <th>Message</th>
    <th>FromCUID</th>
    <th>ToCUID</th>
  </tr>
  <tr>
END;
    echo '<td>'.htmlentities($msg['MESSAGEID']).'</td>';
    echo '<td>'.htmlentities($msg['MESG']).'</td>';
    echo '<td>'.htmlentities($msg['FROMCUID']).'</td>';
    echo '<td>'.htmlentities($msg['TOCUID']).'</td>';
    echo <<<END
  </tr>
  </table>
END;
  }
}


?>